<?php
/**
 * Copyright (c) 2008 Pascal MARTIN <contact@pascal-martin.fr>,
 *                    Rémi PERROUD <remi.perroud@gmail.com>
 *      http://code.google.com/p/simpleguestbook/
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 *
 * @author Pascal MARTIN
 * @version $Id: bootstrap.php 21 2008-09-26 05:33:53Z pascal.martin.pmn $
 *
 */

define('APP_DIR', realpath(dirname(__FILE__)));
define('WEB_DIR', APP_DIR . '/../www/');
define('LIB_DIR', APP_DIR . '/../library/');
define('CONFIG_DIR', APP_DIR . '/');
define('CACHE_DIR', APP_DIR . '/../data/cache/');

class Launcher
{
    
    public static function init()
    {
        error_reporting(E_ALL);
        ini_set('display_errors', 'On');
        
        set_include_path(LIB_DIR
            . PATH_SEPARATOR . realpath(APP_DIR . '/models/')
            . PATH_SEPARATOR . realpath(APP_DIR)
        );
        
        require_once('Zend/Loader.php');
        Zend_Loader::registerAutoload();
        
        $config = new Zend_Config_Ini(CONFIG_DIR . 'config.ini');
        Zend_Registry::set('config', $config);
        
        date_default_timezone_set('Europe/Paris');
        
        $frontController = Zend_Controller_Front::getInstance();
        
        $frontController->returnResponse(false);
        
        $frontController->throwExceptions(true);
        
        $frontController->setParam('noErrorHandler', false);
        $frontController->setParam('disableOutputBuffering', false);
        //$frontController->setParam('noViewRenderer', true);
        
        $frontController->registerPlugin(new Zend_Controller_Plugin_ErrorHandler(
            array(
                'module'     => 'default',
                'controller' => 'error',
                'action'     => 'error'
            )));
        
        $frontController->setControllerDirectory(array(
                'default' => APP_DIR . '/controllers/',
            ));
        
        Zend_Controller_Front::getInstance()->getRouter()->addRoutes(array(
            
            'index'=> new Zend_Controller_Router_Route(
                '/:p',
                array(
                    'module' => 'default',
                    'controller' => 'guestbook',
                    'action'     => 'list',
                    'p' => 1
                ),
                array('p' => '\d+')
            ),
            'adminLogin' => new Zend_Controller_Router_Route_Static(
                'admin/login',
                array(
                    'module' => 'default',
                    'controller' => 'admin',
                    'action' => 'login'
                )
            ),
            'adminLogout' => new Zend_Controller_Router_Route_Static(
                'admin/logout',
                array(
                    'module' => 'default',
                    'controller' => 'admin',
                    'action' => 'logout'
                )
            ),
            'adminNew' => new Zend_Controller_Router_Route_Static(
                'admin/new',
                array(
                    'module' => 'default',
                    'controller' => 'admin',
                    'action' => 'new'
                )
            ),
        ));
        
        
        
        $db = Zend_Db::factory('Pdo_Sqlite', $config->database);
        Zend_Db_Table_Abstract::setDefaultAdapter($db);
        Zend_Registry::set('db', $db);
        
        Zend_Session::start();
        Zend_Auth::getInstance()->setStorage(new Zend_Auth_Storage_Session('user'));
        
        return $frontController;
        
    } // init
    
    
    public static function launch()
    {
        $frontController = self::init();
        $frontController->dispatch();
    }
    
} // class Launcher
